package services

import (
	"fmt"
	jsoniter "github.com/json-iterator/go"
	"io/ioutil"
)

type WebServiceConfig struct {
	Port    uint32 `json:"port"`
	Address string `json:"address"`
}

type DatabaseConfig struct {
	Port      uint32 `json:"port"`
	Address   string `json:"address"`
	Type      string `json:"type"`
	UserName  string `json:"user_name"`
	Password  string `json:"password"`
	TableName string `json:"table_name"`
}
type CookieConfig struct {
	Key string `json:"key"`
}
type PathConfig struct {
	Public string `json:"public"`
}
type ConfigService struct {
	Web      WebServiceConfig `json:"web"`
	Database DatabaseConfig   `json:"database"`
	Cookie   CookieConfig     `json:"cookie"`
	Path     PathConfig       `json:"path"`
}

var ConfigServiceInstance = ConfigService{}

func init() {
	configPaths := []string{"config.json", "config.default.json"}
	for _, item := range configPaths {
		stream, err := ioutil.ReadFile(item)
		if err != nil {
			continue
		}
		cs := ConfigService{}
		err = jsoniter.Unmarshal(stream, &cs)
		if err != nil {
			fmt.Printf("json parser error :%v", err)
			continue
		}
		ConfigServiceInstance = cs
		break
	}
}
